package Text;

import Domain.UserBean;
import DAO.DoForUser;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.RegisForJdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class TextForJdbc {

    /**
     * 创建一个测试类，测试是否与数据库连接成功，并能否对数据库进行修改。结果为成功!!!
     * @throws SQLException
     */
    @Test
    public void textConn() throws SQLException {
        Connection connection = RegisForJdbc.getConnection();
        Statement statement = connection.createStatement();
        String sql ="update user set user_email = 5 where user_id =1";
        int i= statement.executeUpdate(sql);
        System.out.println(i);


    }

    /**
     * 创建一个测试类，测试Dao层的注册，结果为注册成功，传入一个对象(非空),可以将数据插入数据库
     */
    @Test
    public void TextRegister(){
        boolean b = DoForUser.doRegister(new UserBean(null, "李岳", "123456", "12345789", "湖北", "0", "男"));
        Assert.assertEquals(true,b);
    }

    /**
     * 创建一个测试类，对用户名和密码进行测试，看是否正确
     */
    @Test
    public void TextCheckUser(){
        JdbcTemplate template = new JdbcTemplate(RegisForJdbc.getDataSource());
        //UserBean user = new UserBean(null,"1965492170", "132465",null,null,null,null);
        //UserBean userBean = DoForUser.checkUser(user);
        String sql = "select * from user where user_name = ? and user_password = ?";
        UserBean userBean = template.queryForObject(sql, new BeanPropertyRowMapper<UserBean>(UserBean.class), "1965492170", "123456");
        System.out.println(userBean);

    }

    @Test
    public void TextCheckRegister(){
        UserBean user = new UserBean(null,"123456","123456","182715311","湖北","@aa",null);
        boolean b = DoForUser.doRegister(user);
        System.out.println(b);

    }

    }



